<?php
function SVC_FUNC_ADD($request)
{
    if(($dbconn=worker::get_dbconn('DB1')) == false)
        return false;
    $respond='';

    $curr=0;
    $loginname=para::get_item($request, $curr);
    if(empty($loginname) || strlen($loginname) > misc::NAME_SIZE)
    {
        para::put_item($respond, SOAR_ERR::PARA);
        return $respond;
    }

//    try
//    {
//        $stmt=$dbconn->prepare("insert into memb_info values(".
//                "0,sysdate(),sysdate(),:loginname,:nickname,0)");
//        $stmt->execute(array(':loginname'=>$loginname,
//                    ':nickname'=>$loginname));
//    }
//    catch(PDOException $e)
//    {
//        if($e->errorInfo[1] != MYSQL_ERR::UNIQUE_VIOLATE)
//        {
//            errlog::add("%s: worker(%d) insert into memb_info fail: %s",
//                    __METHOD__, worker::$worker_idx,
//                    $e->getMessage()); 
//            $respond=false;
//        }
//        else
//            para::put_item($respond, "MEMB_EXIST");
//        return $respond;
//    }
//    para::put_item($respond, SOAR_ERR::OK);

    if($dbconn->query("update seq_memb set id=last_insert_id(id+1)") ===
            false)
    {
        errlog::add("%s: worker(%d) update seq_memb fail: %d %s",
                __METHOD__, worker::$worker_idx,
                $dbconn->errno, $dbconn->error);
        return false;
    }
    $uuid=$dbconn->insert_id;
    $stmt=$dbconn->prepare("insert into memb_info values(".
            "?,sysdate(),sysdate(),?,?,0)");
    if($stmt === false)
    {
        errlog::add("%s: worker(%d) prepare insert memb_info fail: %d %s",
                __METHOD__, worker::$worker_idx,
                $dbconn->errno, $dbconn->error);
        return false;
    }
    $stmt->bind_param('iss', $uuid, $loginname, $loginname);
    if($stmt->execute() == false)
    {
        if($stmt->errno != MYSQL_ERR::UNIQUE_VIOLATE)
        {
            errlog::add("%s: worker(%d) insert memb_info fail: %d %s",
                    __METHOD__, worker::$worker_idx,
                     $stmt->errno, $stmt->error);
            $stmt->close();
            return false;
        }
        para::put_item($respond, "MEMB_EXIST");
    }
    else
    {
        para::put_item($respond, SOAR_ERR::OK);
    }
    $stmt->close();

    return $respond;
}
?>
